// component/order.js
var app = getApp()

import {
  summary,
} from '../../api/order'

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    shipped: 0,
    unshipping: 0,
    completed: 0,
    unpaid: 0,
    refunds: 0,
    showShipped: 0,
    showUnshipping: 0,
    showCompleted: 0,
    showUnpaid: 0,
    showRefunds: 0
  },

  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      //可视窗口x,y坐标
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  pageLifetimes: {
    show: function() {
      // 页面被展示
      console.info("页面被展示了")
      this.getOrderCount()
    },
    hide: function() {
      // 页面被隐藏
    },
    resize: function(size) {
      // 页面尺寸变化
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //获取订单数量信息
    getOrderCount() {
      summary().then(
        res => {
          console.log("接收订单数量信息--" + JSON.stringify(res))
          if (res.type == 'success') {
            this.setData({
              shipped: res.data.shipped,
              unshipping: res.data.unshipping,
              completed: res.data.completed,
              unpaid: res.data.unpaid,
              refunds: res.data.refunds,
              showShipped: res.data.shipped > 99 ? '...' : res.data.shipped,
              showUnshipping: res.data.unshipping > 99 ? '...' : res.data.unshipping,
              showCompleted: res.data.completed > 99 ? '...' : res.data.completed,
              showUnpaid: res.data.unpaid > 99 ? '...' : res.data.unpaid,
              showRefunds: res.data.refunds > 99 ? '...' : res.data.refunds
            })
          }
        }
      ).catch(error => {
        console.log(error)
      })
    },

    //跳转到所有的订单页面
    toOrder: function(e) {
      this.checkLogin(() => {
        let param = e.currentTarget.dataset.index
        console.info(param)
        wx.navigateTo({
          url: '/pages/member/order/index?index=' + 0,
        })
      })
    },
    toOrderRefund: function() {
      this.checkLogin(() => {
        wx.navigateTo({
          url: '/pages/member/refund/list',
        })
      })

    },
    toUnpaid: function(e) {
      this.checkLogin(() => {
        wx.navigateTo({
          url: '/pages/member/order/index?index=' + 1,
        })
      })

    },
    toNotDelivered: function(e) {
      this.checkLogin(() => {
        wx.navigateTo({
          url: '/pages/member/order/index?index=' + 2,
        })
      })
    },
    toBeReceived: function(e) {
      this.checkLogin(() => {
        wx.navigateTo({
          url: '/pages/member/order/index?index=' + 3,
        })
      })
    },
    toEvaluate: function(e) {
      this.checkLogin(() => {
        wx.navigateTo({
          url: '/pages/member/order/index?index=' + 4,
        })
      })
    },
    //检测用户的登陆状态
    checkLogin(callback) {
      console.info("全局变量app.globalData--" + JSON.stringify(app.globalData))
      if (app.globalData.token == null) {
        wx.navigateTo({
          url: '/pages/login/index',
        })
      } else {
        if (callback) {
          callback()
        }
      }
    },
  }
})